<?php
class Cms
{
  static public function slugify($text)
  {
    // replace non letter or digits by -
    $text = preg_replace('#[^\\pL\d]+#u', '-', $text);

    // trim
    $text = trim($text, '-');

    // transliterate
    if (function_exists('iconv'))
    {
      $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    }

    // lowercase
    $text = strtolower($text);

    // remove unwanted characters
    $text = preg_replace('#[^-\w]+#', '', $text);

    if (empty($text))
    {
      return 'n-a';
    }

    return $text;
  } 
  
  /**
   * Metoda służca do łączenia danych w pary właściwośći pola - wartość
   * @param array $headers właściwości pola
   * @param array $values  wartości wyciągnięte z bazy
   * @param sfRequest $request rządanie
   * @return array połączone pary właściwości - wartość
   * TODO: Tą metodę trzeba gruntownie przeanalizować. Za duzo tu kombinacji z przeszukiwaniem tablic tylko po to zeby ulozyc wszystko w odpowiedniej kolejnosci. 
   */
  public static function bindHedersWithValues($headers = array(), $values = array(), sfRequest $request)
  {
    sfProjectConfiguration::getActive()->loadHelpers(array('I18N'));     
    
    $results = array();
    
    foreach($headers as $key => $header)
    {         
      if(!$header['col'] && $header['tab'])
      {
        $newHeaders[$header['tab']] = $header;
        
        foreach($values as $rowKey => $row)
        {           
          $values[$rowKey][$header['tab']] = null;           
        }
      }
      else
      {
        $newHeaders[$header['col']] = $header;
      }
    }
    
    foreach($values as $rowKey => $row)
    {
      foreach($row as $colKey => $col)
      {
        $results[$rowKey][$colKey] = $newHeaders[$colKey];
 
   
        if($results[$rowKey][$colKey]['cmsFieldType']['name'] == 'boolean')
        {
          $results[$rowKey][$colKey]['value'] = ($col == true ? __('yes'): __('no'));
        }
        else if($results[$rowKey][$colKey]['cmsFieldType']['name'] == 'media')
        {      
          if($results[$rowKey][$colKey]['subtype'] == 'images' || $results[$rowKey][$colKey]['subtype'] == 'files')
          {
            $query = Doctrine_Core::getTable($results[$rowKey][$colKey]['tab'])->createQuery('q')->select('*')->where('q.parent_id = ?', $row['id']);
            
            if($request->getParameter('action') === 'index')
            {
              $query->limit(4);
            }
            
            $value = $query->fetchArray();

          }
          else
          {
            $value = Doctrine_Core::getTable($results[$rowKey][$colKey]['relation'])->find($col);            
          }
          
          $results[$rowKey][$colKey]['value'] = $value;
        }
        else
        {
          $results[$rowKey][$colKey]['value'] = ($request->getParameter('action') === 'index' ? strip_tags($col) : $col);
        }        
      }
    }
    
    foreach($headers as $headerKey => $header)
    {
        $index = ($header['tab'] ? $header['tab'] : $header['col']);
        
        foreach($results as $rowKey => $row)
        {
          $bindedPairsInOrder[$rowKey][$index] = $results[$rowKey][$index];
        }        
    }
    
    return $bindedPairsInOrder;
  }
  
  public static function getFormFieldsArray()
  {
    $fields = cmsFieldTable::getInstance()->getHeadersQuery()->andWhere('c.form = ?', true)->fetchArray();
    
    $newFields = array();
    
    foreach($fields as $field)
    {
      if($field['controle'] == true)
      {
        $newFields['controle'][] = $field;
      }
      else
      {
        $newFields['content'][] = $field;
      }
    }
       
    return $newFields;
  }
  
  public static function getRelationNameFormLocal($tableName, $localName)
  {
    $relations =  Doctrine_Core::getTable($tableName)->getRelations();
    
    foreach ($relations as $relationName => $relation)
    {
      if($relation->getLocal() ==  $localName)
      {
        return $relationName;
      }          
    }
    
    return null;
  }
  
}
?>